package com.podcstr.controller;

import com.podcstr.entity.MediaFile;
import com.podcstr.entity.User;
import com.podcstr.entity.UserProfile;
import com.podcstr.qualifier.CurrentSession;
import com.podcstr.service.UserService;
import com.podcstr.session.UserSession;
import org.apache.log4j.Logger;
import org.primefaces.model.UploadedFile;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.enterprise.context.SessionScoped;
import javax.inject.Inject;
import javax.inject.Named;
import java.io.Serializable;

/**
 * @author azim
 * @since 5/12/13 
 */

@Named
@SessionScoped
public class UserController implements Serializable {

    private final Logger log = Logger.getLogger(UserController.class.getName());

    private User user;
    private UserProfile userProfile;
    private UploadedFile uploadedPhoto;

    @Inject
    @CurrentSession
    private UserSession userSession;

    @EJB
    private UserService userService;

    @PostConstruct
    private void init() {

        if (user == null) {

            user = new User();
            userProfile = new UserProfile();
        }
    }

    //Getters And setters Methods for User Controller

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public UserProfile getUserProfile() {
        return userProfile;
    }

    public void setUserProfile(UserProfile userProfile) {
        this.userProfile = userProfile;
    }

    public void setUserService(UserService userService) {
        this.userService = userService;
    }

    public UploadedFile getUploadedPhoto() {
        return uploadedPhoto;
    }

    public void setUploadedPhoto(UploadedFile uploadedPhoto) {
        this.uploadedPhoto = uploadedPhoto;
    }


    // Other required methods for User Controller

    public String showUserDetails(String username) {

        if (username == null || username == "") {

            user = userSession.getUser();
        } else {

            user = userService.findUserByUsername(username);
        }

        return "login_show_user_profile.xhtml?faces-redirect=true";
    }

    public String UserProfileForm() {

        user = userSession.getUser();
        userProfile = user.getUserProfile();

        if (user != null && userProfile != null) {

            return "login_update_user_profile.xhtml";
        }

        return null;
    }

    public String updateProfile() {

        User previousUser = userSession.getUser();
        UserProfile previousProfile = previousUser.getUserProfile();
        MediaFile previousPhoto = previousProfile.getPhoto();

        if (user != null && userProfile != null && previousUser != null && previousProfile != null) {

            previousUser.setEmail(user.getEmail());

            previousProfile.setFirstName(userProfile.getFirstName());
            previousProfile.setLastName(userProfile.getLastName());
            previousProfile.setContactNo(userProfile.getContactNo());
            previousProfile.setAddress(userProfile.getAddress());

            if (uploadedPhoto != null) {
                previousPhoto.setName(uploadedPhoto.getFileName());
                previousPhoto.setContentType(uploadedPhoto.getContentType());
                previousPhoto.setContent(uploadedPhoto.getContents());
            }
            userService.update(previousUser);
            userService.updateProfile(previousProfile);
            userService.updateFile(previousPhoto);
        }

        return "login_show_user_profile.xhtml?faces-redirect=true";
    }

}
